package cn.iocoder.yudao.module.exam.controller.admin.uploadbatch;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.uploadbatch.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.uploadbatch.UploadBatchDO;
import cn.iocoder.yudao.module.exam.service.uploadbatch.UploadBatchService;

@Tag(name = "管理后台 - 终端管理")
@RestController
@RequestMapping("/exam/upload-batch")
@Validated
public class UploadBatchController {

    @Resource
    private UploadBatchService uploadBatchService;

    @PostMapping("/create")
    @Operation(summary = "创建终端管理")
    @PreAuthorize("@ss.hasPermission('exam:upload-batch:create')")
    public CommonResult<Long> createUploadBatch(@Valid @RequestBody UploadBatchSaveReqVO createReqVO) {
        return success(uploadBatchService.createUploadBatch(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新终端管理")
    @PreAuthorize("@ss.hasPermission('exam:upload-batch:update')")
    public CommonResult<Boolean> updateUploadBatch(@Valid @RequestBody UploadBatchSaveReqVO updateReqVO) {
        uploadBatchService.updateUploadBatch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除终端管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:upload-batch:delete')")
    public CommonResult<Boolean> deleteUploadBatch(@RequestParam("id") Long id) {
        uploadBatchService.deleteUploadBatch(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除终端管理")
                @PreAuthorize("@ss.hasPermission('exam:upload-batch:delete')")
    public CommonResult<Boolean> deleteUploadBatchList(@RequestParam("ids") List<Long> ids) {
        uploadBatchService.deleteUploadBatchListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得终端管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:upload-batch:query')")
    public CommonResult<UploadBatchRespVO> getUploadBatch(@RequestParam("id") Long id) {
        UploadBatchDO uploadBatch = uploadBatchService.getUploadBatch(id);
        return success(BeanUtils.toBean(uploadBatch, UploadBatchRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得终端管理分页")
    @PreAuthorize("@ss.hasPermission('exam:upload-batch:query')")
    public CommonResult<PageResult<UploadBatchRespVO>> getUploadBatchPage(@Valid UploadBatchPageReqVO pageReqVO) {
        PageResult<UploadBatchDO> pageResult = uploadBatchService.getUploadBatchPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UploadBatchRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出终端管理 Excel")
    @PreAuthorize("@ss.hasPermission('exam:upload-batch:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUploadBatchExcel(@Valid UploadBatchPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UploadBatchDO> list = uploadBatchService.getUploadBatchPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "终端管理.xls", "数据", UploadBatchRespVO.class,
                        BeanUtils.toBean(list, UploadBatchRespVO.class));
    }

}